// services/storage.ts - 存储管理 API 服务
import apiClient from './api';
import type {
  StoragePoolDetail,
  StoragePoolSummary,
  StorageVolumeDetail,
  StorageVolumeSummary,
  CreateVolumeRequest,
  ListResponse,
  ApiResponse,
} from '../types/api';

// ========== 存储池管理 ==========

/**
 * 获取存储池列表
 * @param connection 连接ID
 */
export const listStoragePools = async (
  connection: string
): Promise<ListResponse<StoragePoolSummary>> => {
  const response = await apiClient.get<ListResponse<StoragePoolSummary>>('/storage/pools', {
    params: { connection },
  });
  return response.data;
};

/**
 * 获取存储池详情
 * @param connection 连接ID
 * @param pool 存储池名称
 */
export const getStoragePoolDetail = async (
  connection: string,
  pool: string
): Promise<StoragePoolDetail> => {
  const response = await apiClient.get<StoragePoolDetail>('/storage/pool', {
    params: { connection, pool },
  });
  return response.data;
};

/**
 * 创建存储池
 * @param connection 连接ID
 * @param xmlConfig 存储池 XML 配置
 */
export const createStoragePool = async (
  connection: string,
  xmlConfig: string
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/storage/pools', xmlConfig, {
    params: { connection },
    headers: { 'Content-Type': 'text/xml' },
  });
  return response.data;
};

/**
 * 删除存储池
 * @param connection 连接ID
 * @param pool 存储池名称
 */
export const deleteStoragePool = async (
  connection: string,
  pool: string
): Promise<ApiResponse> => {
  const response = await apiClient.delete<ApiResponse>('/storage/pool', {
    params: { connection, pool },
  });
  return response.data;
};

/**
 * 启动存储池
 * @param connection 连接ID
 * @param pool 存储池名称
 */
export const startStoragePool = async (
  connection: string,
  pool: string
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/storage/pool/start', null, {
    params: { connection, pool },
  });
  return response.data;
};

/**
 * 停止存储池
 * @param connection 连接ID
 * @param pool 存储池名称
 */
export const stopStoragePool = async (
  connection: string,
  pool: string
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/storage/pool/stop', null, {
    params: { connection, pool },
  });
  return response.data;
};

// ========== 存储卷管理 ==========

/**
 * 获取存储卷列表
 * @param connection 连接ID
 * @param pool 存储池名称
 */
export const listStorageVolumes = async (
  connection: string,
  pool: string
): Promise<ListResponse<StorageVolumeSummary>> => {
  const response = await apiClient.get<ListResponse<StorageVolumeSummary>>('/storage/volumes', {
    params: { connection, pool },
  });
  return response.data;
};

/**
 * 获取存储卷详情
 * @param connection 连接ID
 * @param pool 存储池名称
 * @param volume 存储卷名称
 */
export const getStorageVolumeDetail = async (
  connection: string,
  pool: string,
  volume: string
): Promise<StorageVolumeDetail> => {
  const response = await apiClient.get<StorageVolumeDetail>('/storage/volume', {
    params: { connection, pool, volume },
  });
  return response.data;
};

/**
 * 创建存储卷
 * @param connection 连接ID
 * @param pool 存储池名称
 * @param volumeConfig 存储卷配置
 */
export const createStorageVolume = async (
  connection: string,
  pool: string,
  volumeConfig: CreateVolumeRequest
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/storage/volumes', volumeConfig, {
    params: { connection, pool },
  });
  return response.data;
};

/**
 * 删除存储卷
 * @param connection 连接ID
 * @param pool 存储池名称
 * @param volume 存储卷名称
 */
export const deleteStorageVolume = async (
  connection: string,
  pool: string,
  volume: string
): Promise<ApiResponse> => {
  const response = await apiClient.delete<ApiResponse>('/storage/volume', {
    params: { connection, pool, volume },
  });
  return response.data;
};

// 导出所有服务
export const storageService = {
  // 存储池
  listStoragePools,
  getStoragePoolDetail,
  createStoragePool,
  deleteStoragePool,
  startStoragePool,
  stopStoragePool,
  // 存储卷
  listStorageVolumes,
  getStorageVolumeDetail,
  createStorageVolume,
  deleteStorageVolume,
};